import pymysql

from shop.conf import sql_conf


class MysqlHandler:
    def __init__(self, host=sql_conf.get("SqlHost"), port=sql_conf.get("SqlPort"), user=sql_conf.get("SqlUser"),
                 passwd=sql_conf.get("SqlPassword"),
                 db="bigbiy_web", charset="utf8mb4"):
        self.conn = pymysql.connect(host=host, port=int(port), user=user, passwd=passwd, db=db, charset=charset)
        self.cursor = self.conn.cursor()

    def fetchone(self, sql_text):
        self.cursor.execute(sql_text)
        return self.cursor.fetchone()

    def fetchall(self, sql_text):
        self.cursor.execute(sql_text)
        return self.cursor.fetchall()

    def update(self, sql_text):
        self.cursor.execute(sql_text)
        self.conn.commit()

    def __del__(self):
        self.cursor.close()
        self.conn.close()
